import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class ManagerMenu extends JFrame {

    private JTextField nameField;
    private JTextField birthField;
    private JTextField genderField;
    private JTextField collegeField;
    private JTextField majorField;
    private JTextField sidField;
    private JTextField tidField;

    public static ArrayList<Student> studentList = new ArrayList<>();
    public static ArrayList<Teacher> teacherList = new ArrayList<>();

    public ManagerMenu() {
        setTitle("管理员菜单");
        setSize(600, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        JPanel selectionPanel = new JPanel();
        selectionPanel.setLayout(new GridLayout(3, 1));

        JButton studentButton = new JButton("管理学生");
        studentButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                showStudentMenu();
            }
        });
        selectionPanel.add(studentButton);

        JButton teacherButton = new JButton("管理教师");
        teacherButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                showTeacherMenu();
            }
        });
        selectionPanel.add(teacherButton);

        JButton exitButton = new JButton("退出");
        exitButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();  // 关闭窗口
            }
        });
        selectionPanel.add(exitButton);

        add(selectionPanel);
        setVisible(true);
    }

    private void showStudentMenu() {
        JFrame studentFrame = new JFrame("管理学生");
        studentFrame.setSize(600, 400);
        studentFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        studentFrame.setLocationRelativeTo(null);

        JPanel studentPanel = new JPanel();
        studentPanel.setLayout(new GridLayout(1, 3));

        JButton addButton = new JButton("添加学生");
        addButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 打开添加学生界面
                JFrame addStudentFrame = new JFrame("添加学生");
                addStudentFrame.setSize(400, 300);
                addStudentFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                addStudentFrame.setLocationRelativeTo(null);

                JPanel addStudentPanel = new JPanel();
                addStudentPanel.setLayout(new GridLayout(7, 2));

                JLabel nameLabel = new JLabel("姓名:");
                nameField = new JTextField(20);
                addStudentPanel.add(nameLabel);
                addStudentPanel.add(nameField);

                JLabel sidLabel = new JLabel("学号:");
                sidField = new JTextField(20);
                addStudentPanel.add(sidLabel);
                addStudentPanel.add(sidField);

                JLabel birthLabel = new JLabel("出生日期:");
                birthField = new JTextField(20);
                addStudentPanel.add(birthLabel);
                addStudentPanel.add(birthField);

                JLabel genderLabel = new JLabel("性别:");
                genderField = new JTextField(20);
                addStudentPanel.add(genderLabel);
                addStudentPanel.add(genderField);

                JLabel collegeLabel = new JLabel("学院:");
                collegeField = new JTextField(20);
                addStudentPanel.add(collegeLabel);
                addStudentPanel.add(collegeField);

                JLabel majorLabel = new JLabel("专业:");
                majorField = new JTextField(20);
                addStudentPanel.add(majorLabel);
                addStudentPanel.add(majorField);

                JButton confirmButton = new JButton("确认");
                confirmButton.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        String name = nameField.getText().trim();
                        String sID = sidField.getText().trim();
                        String birth = birthField.getText().trim();
                        String gender = genderField.getText().trim();
                        String college = collegeField.getText().trim();
                        String major = majorField.getText().trim();

                        // 检查输入数据的合法性
                        if (name.isEmpty() || sID.isEmpty() || birth.isEmpty() || gender.isEmpty() || college.isEmpty() || major.isEmpty()) {
                            JOptionPane.showMessageDialog(addStudentFrame, "请输入所有信息！", "错误", JOptionPane.ERROR_MESSAGE);
                            return;
                        }

                        // 创建新的学生对象并添加到列表中
                        Student newStudent = new Student(name, sID, birth, gender, college, major);
                        studentList.add(newStudent);

                        // 将新学生信息保存到持久化存储中
                        // ...

                        JOptionPane.showMessageDialog(addStudentFrame, "学生添加成功！", "成功", JOptionPane.INFORMATION_MESSAGE);

                        // 清空输入框
                        nameField.setText("");
                        sidField.setText("");
                        birthField.setText("");
                        genderField.setText("");
                        collegeField.setText("");
                        majorField.setText("");
                    }
                });
                addStudentPanel.add(confirmButton);

                addStudentFrame.add(addStudentPanel);
                addStudentFrame.setVisible(true);
            }
        });
        studentPanel.add(addButton);

        JButton modifyButton = new JButton("修改学生信息");
        modifyButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String studentID = JOptionPane.showInputDialog(studentFrame, "请输入要修改的学生学号:");
                if (studentID != null && !studentID.trim().isEmpty()) {
                    boolean found = false;
                    for (Student student : studentList) {
                        if (student.getSID().equals(studentID)) {
                            found = true;

                            // 显示学生信息并让用户修改
                            JFrame modifyStudentFrame = new JFrame("修改学生信息");
                            modifyStudentFrame.setSize(400, 300);
                            modifyStudentFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                            modifyStudentFrame.setLocationRelativeTo(null);

                            JPanel modifyStudentPanel = new JPanel();
                            modifyStudentPanel.setLayout(new GridLayout(7, 2));

                            JLabel nameLabel = new JLabel("姓名:");
                            nameField = new JTextField(student.getName());
                            modifyStudentPanel.add(nameLabel);
                            modifyStudentPanel.add(nameField);

                            JLabel sidLabel = new JLabel("学号:");
                            sidField = new JTextField(student.getSID());
                            modifyStudentPanel.add(sidLabel);
                            modifyStudentPanel.add(sidField);

                            JLabel birthLabel = new JLabel("出生日期:");
                            birthField = new JTextField(student.getBirth());
                            modifyStudentPanel.add(birthLabel);
                            modifyStudentPanel.add(birthField);

                            JLabel genderLabel = new JLabel("性别:");
                            genderField = new JTextField(student.getGender());
                            modifyStudentPanel.add(genderLabel);
                            modifyStudentPanel.add(genderField);

                            JLabel collegeLabel = new JLabel("学院:");
                            collegeField = new JTextField(student.getCollege());
                            modifyStudentPanel.add(collegeLabel);
                            modifyStudentPanel.add(collegeField);

                            JLabel majorLabel = new JLabel("专业:");
                            majorField = new JTextField(student.getMajor());
                            modifyStudentPanel.add(majorLabel);
                            modifyStudentPanel.add(majorField);

                            JButton confirmButton = new JButton("确认");
                            confirmButton.addActionListener(new ActionListener() {
                                @Override
                                public void actionPerformed(ActionEvent e) {
                                    String name = nameField.getText().trim();
                                    String sID = sidField.getText().trim();
                                    String birth = birthField.getText().trim();
                                    String gender = genderField.getText().trim();
                                    String college = collegeField.getText().trim();
                                    String major = majorField.getText().trim();

                                    // 检查是否正确输入
                                    if (name.isEmpty() || sID.isEmpty() || birth.isEmpty() || gender.isEmpty() || college.isEmpty() || major.isEmpty()) {
                                        JOptionPane.showMessageDialog(modifyStudentFrame, "请输入所有信息！", "错误", JOptionPane.ERROR_MESSAGE);
                                        return;
                                    }

                                    // 更新学生信息
                                    student.setName(name);
                                    student.setSID(sID);
                                    student.setBirth(birth);
                                    student.setGender(gender);
                                    student.setCollege(college);
                                    student.setMajor(major);

                                    // 保存学生信息到存储中
                                    // ...

                                    JOptionPane.showMessageDialog(modifyStudentFrame, "学生信息修改成功！", "成功", JOptionPane.INFORMATION_MESSAGE);

                                    // Close the modify student window and clear input fields
                                    modifyStudentFrame.dispose();
                                    nameField.setText("");
                                    sidField.setText("");
                                    birthField.setText("");
                                    genderField.setText("");
                                    collegeField.setText("");
                                    majorField.setText("");
                                }
                            });
                            modifyStudentPanel.add(confirmButton);

                            modifyStudentFrame.add(modifyStudentPanel);
                            modifyStudentFrame.setVisible(true);

                            break;
                        }
                    }
                    if (!found) {
                        JOptionPane.showMessageDialog(studentFrame, "该学号的学生不存在：" + studentID, "错误", JOptionPane.ERROR_MESSAGE);
                    }
                }
            }
        });
        studentPanel.add(modifyButton);

        JButton deleteButton = new JButton("删除学生");
        deleteButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String studentID = JOptionPane.showInputDialog(studentFrame, "请输入要删除的学生学号:");
                if (studentID != null && !studentID.trim().isEmpty()) {
                    boolean found = false;
                    for (int i = 0; i < studentList.size(); i++) {
                        if (studentList.get(i).getSID().equals(studentID)) {
                            found = true;
                            studentList.remove(i);

                            // 从存储中删除学生信息
                            // ...

                            JOptionPane.showMessageDialog(studentFrame, "学生删除成功！", "成功", JOptionPane.INFORMATION_MESSAGE);

                            break;
                        }
                    }
                    if (!found) {
                        JOptionPane.showMessageDialog(studentFrame, "该学号的学生不存在：" + studentID, "错误", JOptionPane.ERROR_MESSAGE);
                    }
                }
            }
        });
        studentPanel.add(deleteButton);

        studentFrame.add(studentPanel);
        studentFrame.setVisible(true);
    }
    private void showTeacherMenu() {
        JFrame teacherFrame = new JFrame("管理教师");
        teacherFrame.setSize(600, 400);
        teacherFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        teacherFrame.setLocationRelativeTo(null);

        JPanel teacherPanel = new JPanel();
        teacherPanel.setLayout(new GridLayout(1, 2));

        JButton addButton = new JButton("添加教师");
        addButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 打开添加教师界面
                JFrame addTeacherFrame = new JFrame("添加教师");
                addTeacherFrame.setSize(400, 300);
                addTeacherFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                addTeacherFrame.setLocationRelativeTo(null);

                JPanel addTeacherPanel = new JPanel();
                addTeacherPanel.setLayout(new GridLayout(7, 2));

                JLabel nameLabel = new JLabel("姓名：");
                nameField = new JTextField(20);
                addTeacherPanel.add(nameLabel);
                addTeacherPanel.add(nameField);

                JLabel tidLabel = new JLabel("tID：");
                tidField = new JTextField(20);
                addTeacherPanel.add(tidLabel);
                addTeacherPanel.add(tidField);

                JLabel birthLabel = new JLabel("生日：");
                birthField = new JTextField(20);
                addTeacherPanel.add(birthLabel);
                addTeacherPanel.add(birthField);

                JLabel genderLabel = new JLabel("性别：");
                genderField = new JTextField(20);
                addTeacherPanel.add(genderLabel);
                addTeacherPanel.add(genderField);

                JLabel collegeLabel = new JLabel("学院：");
                collegeField = new JTextField(20);
                addTeacherPanel.add(collegeLabel);
                addTeacherPanel.add(collegeField);

                JLabel majorLabel = new JLabel("专业：");
                majorField = new JTextField(20);
                addTeacherPanel.add(majorLabel);
                addTeacherPanel.add(majorField);

                JButton confirmButton = new JButton("确认");
                confirmButton.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        String name = nameField.getText().trim();
                        String tID = tidField.getText().trim();
                        String birth = birthField.getText().trim();
                        String gender = genderField.getText().trim();
                        String college = collegeField.getText().trim();
                        String major = majorField.getText().trim();

                        // 检查输入数据的合法性
                        if (name.isEmpty() || tID.isEmpty() || birth.isEmpty() || gender.isEmpty() || college.isEmpty() || major.isEmpty()) {
                            JOptionPane.showMessageDialog(addTeacherFrame, "请填写所有字段。", "错误", JOptionPane.ERROR_MESSAGE);
                            return;
                        }

                        // 创建新的教师对象并添加到列表中
                        Teacher newTeacher = new Teacher(name, tID, birth, gender, college, major);
                        teacherList.add(newTeacher);

                        // 将新教师信息保存到持久化存储中
                        // ...

                        JOptionPane.showMessageDialog(addTeacherFrame, "教师已成功添加。", "成功", JOptionPane.INFORMATION_MESSAGE);

                        // 清空输入框
                        nameField.setText("");
                        tidField.setText("");
                        birthField.setText("");
                        genderField.setText("");
                        collegeField.setText("");
                        majorField.setText("");
                    }
                });
                addTeacherPanel.add(confirmButton);

                addTeacherFrame.add(addTeacherPanel);
                addTeacherFrame.setVisible(true);
            }
        });
        teacherPanel.add(addButton);

        JButton modifyButton = new JButton("修改教师信息");
        modifyButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String teacherID = JOptionPane.showInputDialog(teacherFrame, "请输入要修改的教师ID：");
                if (teacherID != null && !teacherID.trim().isEmpty()) {
                    boolean found = false;
                    for (Teacher teacher : teacherList) {
                        if (teacher.getTID().equals(teacherID)) {
                            found = true;

                            // 显示教师信息并允许用户修改
                            JFrame modifyTeacherFrame = new JFrame("修改教师信息");
                            modifyTeacherFrame.setSize(400, 300);
                            modifyTeacherFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                            modifyTeacherFrame.setLocationRelativeTo(null);

                            JPanel modifyTeacherPanel = new JPanel();
                            modifyTeacherPanel.setLayout(new GridLayout(7, 2));

                            JLabel nameLabel = new JLabel("姓名：");
                            nameField = new JTextField(teacher.getName());
                            modifyTeacherPanel.add(nameLabel);
                            modifyTeacherPanel.add(nameField);

                            JLabel tidLabel = new JLabel("tID：");
                            tidField = new JTextField(teacher.getTID());
                            modifyTeacherPanel.add(tidLabel);
                            modifyTeacherPanel.add(tidField);

                            JLabel birthLabel = new JLabel("生日：");
                            birthField = new JTextField(teacher.getBirth());
                            modifyTeacherPanel.add(birthLabel);
                            modifyTeacherPanel.add(birthField);

                            JLabel genderLabel = new JLabel("性别：");
                            genderField = new JTextField(teacher.getGender());
                            modifyTeacherPanel.add(genderLabel);
                            modifyTeacherPanel.add(genderField);

                            JLabel collegeLabel = new JLabel("学院：");
                            collegeField = new JTextField(teacher.getCollege());
                            modifyTeacherPanel.add(collegeLabel);
                            modifyTeacherPanel.add(collegeField);

                            JLabel majorLabel = new JLabel("专业：");
                            majorField = new JTextField(teacher.getMajor());
                            modifyTeacherPanel.add(majorLabel);
                            modifyTeacherPanel.add(majorField);

                            JButton confirmButton = new JButton("确认");
                            confirmButton.addActionListener(new ActionListener() {
                                @Override
                                public void actionPerformed(ActionEvent e) {
                                    String name = nameField.getText().trim();
                                    String tID = tidField.getText().trim();
                                    String birth = birthField.getText().trim();
                                    String gender = genderField.getText().trim();
                                    String college = collegeField.getText().trim();
                                    String major = majorField.getText().trim();

                                    // 检查输入数据的合法性
                                    if (name.isEmpty() || tID.isEmpty() || birth.isEmpty() || gender.isEmpty() || college.isEmpty() || major.isEmpty()) {
                                        JOptionPane.showMessageDialog(modifyTeacherFrame, "请填写所有字段。", "错误", JOptionPane.ERROR_MESSAGE);
                                        return;
                                    }

                                    // 更新教师信息
                                    teacher.setName(name);
                                    teacher.setTID(tID);
                                    teacher.setBirth(birth);
                                    teacher.setGender(gender);
                                    teacher.setCollege(college);
                                    teacher.setMajor(major);

                                    // 将修改后的教师信息保存到持久化存储中
                                    // ...

                                    JOptionPane.showMessageDialog(modifyTeacherFrame, "教师信息已成功修改。", "成功", JOptionPane.INFORMATION_MESSAGE);
                                    modifyTeacherFrame.dispose();
                                }
                            });
                            modifyTeacherPanel.add(confirmButton);

                            modifyTeacherFrame.add(modifyTeacherPanel);
                            modifyTeacherFrame.setVisible(true);
                            break;
                        }
                    }
                    if (!found) {
                        JOptionPane.showMessageDialog(teacherFrame, "未找到该教师。", "错误", JOptionPane.ERROR_MESSAGE);
                    }
                }
            }
        });
        teacherPanel.add(modifyButton);

        JButton deleteButton = new JButton("删除教师信息");
        deleteButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String teacherID = JOptionPane.showInputDialog(teacherFrame, "请输入要删除的教师ID：");
                if (teacherID != null && !teacherID.trim().isEmpty()) {
                    boolean found = false;
                    for (int i = 0; i < teacherList.size(); i++) {
                        if (teacherList.get(i).getTID().equals(teacherID)) {
                            found = true;
                            teacherList.remove(i);

                            // 将修改后的教师列表保存到持久化存储中
                            // ...

                            JOptionPane.showMessageDialog(teacherFrame, "教师已成功删除。", "成功", JOptionPane.INFORMATION_MESSAGE);
                            break;
                        }
                    }
                    if (!found) {
                        JOptionPane.showMessageDialog(teacherFrame, "未找到该教师。", "错误", JOptionPane.ERROR_MESSAGE);
                    }
                }
            }
        });
        teacherPanel.add(deleteButton);

        teacherFrame.add(teacherPanel);
        teacherFrame.setVisible(true);
    }
}